<template>
  <div class="lz-table">
    <div class="header">
      <slot name="header">
        <div class="title">
          <slot name="headerLeft"></slot>
        </div>
        <div class="middle">
          <slot name="headerMiddle"></slot>
        </div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
      @cell-dblclick="handleDBLClick"
      @header-click="handleContextnemu"
      :max-height="tb_height"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="Index"
        align="center"
        width="80"
      ></el-table-column>
      <template v-for="propItem in propList" :key="propItem.prop">
        <el-table-column
          v-if="!propItem.isHidden"
          v-bind="propItem"
          align="center"
          :show-overflow-tooltip="showTooltip"
        >
          <template #header="">
            <slot :name="propItem.slotHeaderName">{{ propItem.label }}</slot>
          </template>
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <div class="footer_left">
        <slot name="footerLeft"></slot>
      </div>
      <slot name="footer">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 50, 100, 1000]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    title: {
      type: String,
      default: ''
    },
    listData: {
      type: Array,
      required: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    },
    showTooltip: {
      type: Boolean,
      default: true
    },
    showClass: {
      type: Boolean,
      default: false
    }
  },
  emits: ['selectionChange', 'update:page', 'dblclick', 'headerClick'],
  setup(props, { emit }) {
    const handleSelectionChange = (value: any) => {
      emit('selectionChange', value)
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }

    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }

    const handleDBLClick = (row: any, column: any, cell: any, event: any) => {
      emit('dblclick', { row, column, cell, event })
    }

    const handleContextnemu = (column: any, event: any) => {
      emit('headerClick', { column, event })
    }
    // console.log('2222333344445555')
    // console.log(screen.height)
    // console.log(window.innerHeight)
    // console.log(window.outerHeight)
    const tb_height = window.innerHeight - 330
    const tableRowClassName = (row: any) => {
      if (props.showClass) {
        if (row.row.status === 1) {
          return 'green-row'
        } else if (row.row.status === 2) {
          return 'red-row'
        } else if (row.row.status === 3) {
          return 'orange-row'
        }
        return ''
      }
      if (row.row.status === 'Complete') {
        return 'success-row'
      } else if (row.row.status === 'In Progress') {
        return 'warning-row'
      }
      return ''
    }

    return {
      handleSelectionChange,
      handleCurrentChange,
      handleSizeChange,
      handleDBLClick,
      handleContextnemu,
      tb_height,
      tableRowClassName
    }
  }
})
</script>

<style>
.lz-table .el-table .warning-row {
  background: oldlace;
}
.lz-table .el-table .success-row {
  background: #bee6a8;
}
.lz-table .el-table .green-row {
  background: rgb(140, 240, 140);
  color: #000;
}
.lz-table .el-table .red-row {
  background: rgb(238, 89, 89);
  color: #000;
}
.lz-table .el-table .orange-row {
  background: orange;
  color: #000;
}
.el-table td,
.el-table th {
  padding: 4px 0;
}
</style>

<style scoped lang="less">
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .el-pagination {
    text-align: right;
  }
}
</style>
